
using Microsoft.CodeAnalysis.CSharp.Syntax;

namespace RpcCodegen {
	public static class BinaryFormatterHelper {
		public static string GetReadLiteral(TypeSyntax typeLiteral) {
			return $"Read_{TypeLiteralToFormatterType(typeLiteral)}()";
		}

		public static string GetWriteLiteral(TypeSyntax typeLiteral, string arg) {
			return $"Write_{TypeLiteralToFormatterType(typeLiteral)}({arg})";
		}

		public static string TypeLiteralToFormatterType(TypeSyntax type) {
			var typeLiteral = type.ToString();
			if ( type is TypeSyntax ) {
				typeLiteral.Replace("[]", "");
			}
			if ( typeLiteral.StartsWith("System.") ) {
				typeLiteral = typeLiteral.Substring(7);
			}
			switch (typeLiteral) {
				case "boolean":
				case "bool":
				case "Bool":
					return "Bool";
				case "byte":
					return "Byte";
				case "ushort":
				case "UInt16":
					return "Ushort";
				case "uint":
				case "UInt32":
					return "Uint";
				case "ulong":
				case "UInt64":
					return "Ulong";
				case "long":
				case "Int64":
					return "Long";
				case "int":
				case "Int32":
					return "Int";
				case "short":
				case "Int16":
					return "Short";
				case "float":
				case "Single":
					return "Float";
				case "double":
				case "Double":
					return "Double";
				case "string":
				case "String":
					return "String";
				case "byte[]":
					return "Bytes";
				case "Vector2":
					return typeLiteral;
				case "Vector3":
					return typeLiteral;
				case "Vector4":
					return typeLiteral;
				case "Vector2Int":
					return typeLiteral;
				case "Vector3Int":
					return typeLiteral;
				case "Color32":
					return typeLiteral;
				case "Quaternion":
					return typeLiteral;
				case "":
				case null:
					return "None";
				default:
					return $"Unmanaged<{typeLiteral}>";
			}
		}
	}
}
